package com.hogwarts.webui.pageobj;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

public class BingOps {
    public static void inputText(WebDriver driver,  String keyWords){
        WebElement inputTxt = BingPage.inputTxt(driver);
        inputTxt.sendKeys(keyWords);
    }

    public static void clickSearch(WebDriver driver){
        WebElement searchIcon = BingPage.searchIcon(driver);
        searchIcon.click();
    }

    public static void takeScreenshot(WebDriver driver, String imgPath) throws IOException {
        String base64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
        byte[] decodedBytes = Base64.getDecoder().decode(base64.replace("\n", ""));
        Path file = Paths.get(imgPath);
        Files.createDirectories(file.getParent());
        Files.write(file, decodedBytes);
    }
}
